import { createRouter, createWebHistory } from 'vue-router'
import { useTokenStore } from '@/stores/token.js'
import Login from '../views/Login.vue'
import Dashboard from '../views/Dashboard.vue'
import UserAudit from '../views/UserAudit.vue'
import UserManagement from '../views/UserManagement.vue'
import TeamManagement from '../views/TeamManagement.vue'


import HomepageEditor from '../views/HomepageEditor.vue'
import SystemNotifications from '../views/SystemNotifications.vue'
import PublishNotification from '../views/PublishNotification.vue'
import NewsManagement from '../views/NewsManagement.vue'

const routes = [
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      title: '管理员登录 - 校园智慧竞赛平台'
    }
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: Dashboard,
    meta: {
      title: '管理后台 - 校园智慧竞赛平台',
      requiresAuth: true
    }
  },
  {
    path: '/user-audit',
    name: 'UserAudit',
    component: UserAudit,
    meta: {
      title: '用户审核 - 校园智慧竞赛平台',
      requiresAuth: true
    }
  },
  {
    path: '/user-management',
    name: 'UserManagement',
    component: UserManagement,
    meta: {
      title: '用户管理 - 校园智慧竞赛平台',
      requiresAuth: true
    }
  },


  {
    path: '/homepage-editor',
    name: 'HomepageEditor',
    component: HomepageEditor,
    meta: {
      title: '主页编辑 - 校园智慧竞赛平台',
      requiresAuth: true
    }
  },
  {
    path: '/system-notifications',
    name: 'SystemNotifications',
    component: SystemNotifications,
    meta: {
      title: '系统通知 - 校园智慧竞赛平台',
      requiresAuth: true
    }
  },
  {
    path: '/publish-notification',
    name: 'PublishNotification',
    component: PublishNotification,
    meta: {
      title: '发布通知 - 校园智慧竞赛平台',
      requiresAuth: true
    }
  },
  {
    path: '/edit-notification/:id',
    name: 'EditNotification',
    component: PublishNotification,
    meta: {
      title: '编辑通知 - 校园智慧竞赛平台',
      requiresAuth: true
    }
  },
  {
    path: '/team-management',
    name: 'TeamManagement',
    component: TeamManagement,
    meta: {
      title: '队伍管理 - 校园智慧竞赛平台',
      requiresAuth: true
    }
  },

  {
    path: '/news-management',
    name: 'NewsManagement',
    component: NewsManagement,
    meta: {
      title: '新闻管理 - 校园智慧竞赛平台',
      requiresAuth: true
    }
  },
  {
    path: '/publish-news',
    name: 'PublishNews',
    component: () => import('@/views/PublishNews.vue'),
    meta: {
      title: '发布新闻 - 校园智慧竞赛平台',
      requiresAuth: true
    }
  },
  {
    path: '/edit-news/:id',
    name: 'EditNews',
    component: () => import('@/views/PublishNews.vue'),
    meta: {
      title: '编辑新闻 - 校园智慧竞赛平台',
      requiresAuth: true
    }
  },
  {
    path: '/event-management',
    name: 'EventManagement',
    component: () => import('@/views/EventManagement.vue'),
    meta: {
      title: '赛事管理 - 校园智慧竞赛平台',
      requiresAuth: true
    }
  },
  {
    path: '/publish-event',
    name: 'PublishEvent',
    component: () => import('@/views/PublishEvent.vue'),
    meta: {
      title: '发布赛事 - 校园智慧竞赛平台',
      requiresAuth: true
    }
  },
  {
    path: '/edit-event/:id',
    name: 'EditEvent',
    component: () => import('@/views/PublishEvent.vue'),
    meta: {
      title: '编辑赛事 - 校园智慧竞赛平台',
      requiresAuth: true
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title
  }

  // 检查登录状态
  const tokenStore = useTokenStore()
  const requiresAuth = to.meta.requiresAuth

  if (requiresAuth && !tokenStore.token) {
    // 需要登录但未登录，跳转到登录页
    next('/login')
  } else if (to.path === '/login' && tokenStore.token) {
    // 已登录但访问登录页，跳转到首页
    next('/dashboard')
  } else {
    next()
  }
})

export default router